package cn.wolfcode.crm.service.Impl;

import cn.wolfcode.crm.domain.Buyrecord;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.BuyrecordMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IBuyrecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
@Transactional
public class BuyrecordSreviceImpl implements IBuyrecordService{

    @Autowired
    private BuyrecordMapper buyrecordMapper;
    @Override
    public void save(Buyrecord buyrecord) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        buyrecord.setInputTime(new Date());
        buyrecordMapper.insert(buyrecord);
        buyrecordMapper.updateStatus(buyrecord.getCustomer().getId());
    }

    @Override
    public void delete(Long id) {
        buyrecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Buyrecord buyrecord) {
        buyrecordMapper.updateByPrimaryKey(buyrecord);
    }

    @Override
    public Buyrecord get(Long id) {
        return buyrecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Buyrecord> list() {
        return buyrecordMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Buyrecord> buyrecords = buyrecordMapper.selectForList(qo);
        return new PageInfo(buyrecords);
    }
}
